package com.red.selling.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.red.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.red.selling.domain.RedDeliverySub;
import com.red.selling.mapper.RedDeliveryMapper;
import com.red.selling.domain.RedDelivery;
import com.red.selling.service.IRedDeliveryService;

/**
 * 销售送货单Service业务层处理
 * 
 * @author bug2222
 * @date 2024-11-20
 */
@Service
public class RedDeliveryServiceImpl implements IRedDeliveryService 
{
    @Autowired
    private RedDeliveryMapper redDeliveryMapper;

    /**
     * 查询销售送货单
     * 
     * @param id 销售送货单主键
     * @return 销售送货单
     */
    @Override
    public RedDelivery selectRedDeliveryById(Long id)
    {
        return redDeliveryMapper.selectRedDeliveryById(id);
    }

    /**
     * 查询销售送货单列表
     * 
     * @param redDelivery 销售送货单
     * @return 销售送货单
     */
    @Override
    public List<RedDelivery> selectRedDeliveryList(RedDelivery redDelivery)
    {
        return redDeliveryMapper.selectRedDeliveryList(redDelivery);
    }

    /**
     * 新增销售送货单
     * 
     * @param redDelivery 销售送货单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRedDelivery(RedDelivery redDelivery)
    {
        int rows = redDeliveryMapper.insertRedDelivery(redDelivery);
        insertRedDeliverySub(redDelivery);
        return rows;
    }

    /**
     * 修改销售送货单
     * 
     * @param redDelivery 销售送货单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRedDelivery(RedDelivery redDelivery)
    {
        redDeliveryMapper.deleteRedDeliverySubByDeliveryId(redDelivery.getId());
        insertRedDeliverySub(redDelivery);
        return redDeliveryMapper.updateRedDelivery(redDelivery);
    }

    /**
     * 批量删除销售送货单
     * 
     * @param ids 需要删除的销售送货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRedDeliveryByIds(Long[] ids)
    {
        redDeliveryMapper.deleteRedDeliverySubByDeliveryIds(ids);
        return redDeliveryMapper.deleteRedDeliveryByIds(ids);
    }

    /**
     * 删除销售送货单信息
     * 
     * @param id 销售送货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRedDeliveryById(Long id)
    {
        redDeliveryMapper.deleteRedDeliverySubByDeliveryId(id);
        return redDeliveryMapper.deleteRedDeliveryById(id);
    }

    /**
     * 新增销售送货单子信息
     * 
     * @param redDelivery 销售送货单对象
     */
    public void insertRedDeliverySub(RedDelivery redDelivery)
    {
        List<RedDeliverySub> redDeliverySubList = redDelivery.getRedDeliverySubList();
        Long id = redDelivery.getId();
        if (StringUtils.isNotNull(redDeliverySubList))
        {
            List<RedDeliverySub> list = new ArrayList<RedDeliverySub>();
            for (RedDeliverySub redDeliverySub : redDeliverySubList)
            {
                redDeliverySub.setDeliveryId(id);
                list.add(redDeliverySub);
            }
            if (list.size() > 0)
            {
                redDeliveryMapper.batchRedDeliverySub(list);
            }
        }
    }
}
